package servlet;

import java.io.IOException;
import java.sql.Connection;
//import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;

import dbutil.DBUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class SearchMesagesServlet
 */
@WebServlet("/SearchMesagesServlet")
public class SearchMesagesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchMesagesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unused")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
  		request.setCharacterEncoding("UTF-8");
  		String search =request.getParameter("search");
  		System.out.println(search);
  		JSONArray jsonarray = new JSONArray(); 
  		DBUtil db=new DBUtil();
		Connection conn = db.getConn();
		try {
			String sql;
			if(search.equals("")) {
				System.out.println("NULL");
			}else {
			sql = "select * from newsdetail JOIN types on newsdetail.type = types.tid  where title like '%" +search+"%'";
			ResultSet rs = db.getResult(sql);
			while(rs.next()) {
				JSONObject jsonobj = new JSONObject();
				jsonobj.put("Mid",rs.getInt("Mid"));
  				jsonobj.put("title",rs.getString("title"));
  				jsonobj.put("content",rs.getString("content"));
  				jsonobj.put("author",rs.getString("author"));
  				jsonobj.put("time",rs.getString("time"));
  				jsonobj.put("type",rs.getString("type"));
  				jsonobj.put("tname", rs.getString("tname"));
				jsonarray.put(jsonobj);
			}
			
//			sql = "select * from newsdetail where content like '%" +search+"%'";
//			rs = db.getResult(sql);
//			while(rs.next()) {
//				JSONObject jsonobj = new JSONObject();
//				jsonobj.put("Mid",rs.getInt("Mid"));
//  				jsonobj.put("title",rs.getString("title"));
//  				jsonobj.put("content",rs.getString("content"));
//  				jsonobj.put("author",rs.getString("author"));
//  				jsonobj.put("time",rs.getString("time"));
//  				jsonobj.put("type",rs.getString("type"));
//				jsonarray.put(jsonobj);
//			}
			
			sql = "select * from newsdetail JOIN types on newsdetail.type = types.tid where author like '%" +search+"%'";
			rs = db.getResult(sql);
			while(rs.next()) {
				JSONObject jsonobj = new JSONObject();
				jsonobj.put("Mid",rs.getInt("Mid"));
  				jsonobj.put("title",rs.getString("title"));
  				jsonobj.put("content",rs.getString("content"));
  				jsonobj.put("author",rs.getString("author"));
  				jsonobj.put("time",rs.getString("time"));
  				jsonobj.put("type",rs.getString("type"));
				jsonarray.put(jsonobj);
			}
			}
			
			response.getWriter().println(jsonarray);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(db!=null)
				db.release();//关闭数据库
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
